package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelCommentMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private TravelCommentMapper travelCommentMapper;
    @Override
    public PageInfo query(TravelQueryObject qo) {
        //排序
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrder());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(Travel travel) {
        //默认需要添加的数据
        travel.setCreateTime(new Date());
        travel.setLastUpdateTime(new Date());
        //保存游记的操作必须返回游记的id
        travelMapper.insert(travel);
        //将内容保存
        TravelContent content = travel.getTravelContent();
        User author = travel.getAuthor();
        content.setId(travel.getId());
        travelContentMapper.insert(content);
    }

    @Override
    public Travel getTrave(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        User author = travel.getAuthor();
        author.setFans(travelMapper.getFansS(travel.getId()));
        travel.setAuthor(author);
        return travel;
    }

    @Override
    public void put(Travel travel) {
        //添加默认值最后的更新时间
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);
        //更新内容
        TravelContent travelContent = travel.getTravelContent();
        travelContent.setId(travel.getId());
        travelContentMapper.updateByPrimaryKey(travelContent);
    }

    @Override
    public void updateState(Travel travel) {
        if (travel.getState() == Travel.StATE_RELEASE){
            //如果修改的状态为发布状态,应该更新发布时间
            travel.setReleaseTime(new Date());
        }
        travelMapper.updateState(travel);
    }

    @Override
    public TravelContent getContentByTravelId(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> gettravelsByStrategyId(Long strategyId) {
        return travelMapper.selecttravelsByStrategyId(strategyId);
    }

    @Override
    public void deleteRelation(Long strategyId, Long travelId) {
        travelMapper.deleteRelation(strategyId,travelId);
    }

    @Override
    public List<Travel> getRegionTravelByStrategyId(Long trategyId) {
        return travelMapper.selectRegionTravelByStrategyId(trategyId);
    }

    /**
     * 保存关系
     * @param strategyId
     * @param travelId
     */
    @Override
    public void saveRelation(Long strategyId, Long travelId) {
        travelMapper.saveRelation(strategyId,travelId);
    }

    @Override
    public void subGreatNum(Long travelId) {
        //查询出来该文章的点赞数,将点赞数量减一
        int num= travelMapper.selectGreatNum(travelId);
        //更新文章的点赞数量
        travelMapper.updateGreatNum(travelId,num-1);
    }
    @Override
    public void addGreatNum(Long travelId) {
        int num= travelMapper.selectGreatNum(travelId);
        //更新文章的点赞数量
        travelMapper.updateGreatNum(travelId,num+1);
    }

    @Override
    public void addCommentNum(TravelComment travelComment) {
        Long travelId = travelComment.getTravel().getId();
        int num= travelMapper.selectCommentNum(travelId);
        //更新文章的评论数量
        travelMapper.updateCommentNum(travelId,num+1);
    }

    @Override
    public Object getCommentsByTravelId(Long id) {
        return travelCommentMapper.selectCommentsByTravelId(id);
    }

    @Override
    public int selectGreatByTravelId(Long id) {
        return travelMapper.selectGreatNum(id);
    }

    @Override
    public PageInfo selectTavelsByuserId(TravelQueryObject qo,Long id) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> list = travelMapper.selectTavelsByuserId(id);
        return new PageInfo(list);


    }


}
